<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Hocphi_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_all_hocphi($limit, $start)
	{
		$this->db->select('hocphi.*,user.id as userid, user.username, user.fullname, hocvien.id as hocvienid, hocvien.mahv, hocvien.tenhv');
		$this->db->from('hocphi','user','hocvien');
		$this->db->join('user', 'user.id = hocphi.nguoithu_id');
		$this->db->join('hocvien', 'hocvien.id = hocphi.nguoinop_id');
		$this->db->where('hocphi.deleted',0);
		$this->db->order_by('hocphi.id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}

	function get_all_miengiam($limit, $start)
	{
		$this->db->select('hocvien.*, lophoc.id as lophocid, lophoc.malop,lophoc.tenlop');
		//$this->db->from('hocphi','giaovien','hocvien');
		$this->db->from('hocvien','lophoc');
		//$this->db->join('giaovien', 'giaovien.id = hocphi.nguoithu');
		$this->db->join('lophoc', 'lophoc.id = hocvien.id_lop');
		$this->db->where('hocvien.deleted',0);
		$this->db->where('hocvien.bodoi',1);
		$this->db->order_by('hocvien.id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_tenkhoa()
	{
		$this->db->select('id, makhoa, tenkhoa');
		$this->db->from('khoa');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
    
	public function get_hocphi_info($id)
	{
		$this->db->select('*');
		$this->db->from('hocphi');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function edit_hocphi($id, $sotienthu)
	{
		//$ngaysinh = ($this->input->post('ngaysinh') != '') ? date('Y-m-d', strtotime($this->input->post('ngaysinh'))) : date('Y-m-d');
		
		//$time = mdate('%dd/%mm/%yyyy %H:%m:%s',time());
		$fields = array(
			'sotienthu'			=> $sotienthu,
			'edited_by'			=> $this->user->id,
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('hocphi');
	}
	
	function delete_hocphi($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('hocphi');
	}

	
	public function change_status_giaovien($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('giaovien',$data);		
	}
	
	
	public function search_data($keyword)
	{
		$this->db->select('giaovien.*, khoa.id as khoacid, khoa.makhoa');
		$this->db->from('giaovien','khoa');
		$this->db->join('khoa','khoa.id = giaovien.id_khoa');
		$this->db->where('giaovien.deleted',0);
		$this->db->like('giaovien.magv', $keyword);
		$this->db->or_like('giaovien.cmnd', $keyword);
		$this->db->order_by('giaovien.id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}

	public function count_all_hocphi()
	{
		$this->db->select('id');
		$this->db->from('hocphi');
		return $this->db->count_all_results();
	}
	
	public function count_all_miengiam()
	{
		$this->db->select('id');
		$this->db->where('bodoi',1);
		$this->db->from('hocvien');
		return $this->db->count_all_results();
	}
}
// END giaovien_model.php
/* Location: ./application/models/giaovien_model.php */
